import { loading } from "tdesign-react";
import type { LoadingInstance } from "tdesign-react";

let _loadingInstance: LoadingInstance;

/**
 * 全局loading
 */
const _defaultShowCallback = () => {
  _loadingInstance = loading(true);
};

const _defaultHideCallback = () => {
  _loadingInstance.hide();
};

class Loading {
  private _count: number = 0;
  constructor(private showCallback = _defaultShowCallback, private hideCallback = _defaultHideCallback) {}

  private runCallback() {
    if (this._count > 0) {
      if (this.showCallback) this.showCallback();
      return;
    }
    if (this.hideCallback) {
      this.hideCallback();
    }
  }

  show() {
    this._count++;
    this.runCallback();
  }
  hide() {
    this._count--;
    this.runCallback();
  }
}

export { Loading };
